package com.atguitu.campus.renwendemo.Controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.atguitu.campus.renwendemo.dto.ImageObstacleDto;
import com.atguitu.campus.renwendemo.utils.AjaxResult;
import com.atguitu.campus.renwendemo.utils.HttpUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/img")
public class ImageObstacleController {

    @Value("${upload.path}")
    private String uploadPath;

    @PostMapping("/obstacle")
    public AjaxResult obstacle(@RequestBody ImageObstacleDto dto ) throws IOException {

        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type","application/json");
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("filePath",uploadPath +"/"+dto.getFileName());
        String postResponse = HttpUtils.postJson("http://127.0.0.1:5000/data", jsonBody.toJSONString(),headers);

        JSONObject res = JSONObject.parseObject(postResponse).getJSONObject("data");
        //识别结果文件
        String outFilePath = res.getString("outFilePath");
        JSONArray resArr = res.getJSONArray("resArr").getJSONArray(0);

        JSONObject result = new JSONObject();
        result.put("outFilePath",outFilePath.replaceAll("c:/upload","http://localhost:8080/file"));
        result.put("resArr",resArr);


        return AjaxResult.success("识别完成",result);

    }
}
